package com.ruoyi.web.controller.demand;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ProcuredOrders;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Demand;
import com.ruoyi.system.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


import static com.ruoyi.common.utils.PageUtils.startPage;


@RestController
@RequestMapping("/demand")
public class DemandController extends BaseController {

    @Autowired
    private DemandService demandService;

    /**
     * 查询
     * @param demand
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:list')")
    @GetMapping("/list")
    public TableDataInfo list(Demand demand){
        startPage();
        List<Demand> list = demandService.selectDemandList(demand);
        return getDataTable(list);
    }

    /**
     * 添加
     * @param demand
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Demand demand){
        demand.setAuthor(getNickname());
        demand.setAddTime(DateUtils.getNowDate());
        return toAjax(demandService.insertAll(demand));
    }

    /**
     * 详情
     * @param demandId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:query')")
    @GetMapping(value = "/{demandId}")
    public AjaxResult getInfo(@PathVariable("demandId") Long demandId){
        return success(demandService.selectDemand(demandId));
    }

    /**
     * 修改
     * @param demand
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Demand demand){
        demand.setUpdateTime(DateUtils.getNowDate());
        return toAjax(demandService.updateAll(demand));
    }

    /**
     * 删除
     * @param demandIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:remove')")
    @DeleteMapping("/{demandIds}")
    public AjaxResult remove(@PathVariable Long[] demandIds){
        return toAjax(demandService.updateByDemandId(demandIds));
    }

    /**
     * 回复
     * @param demand
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:reply')")
    @PutMapping("/replyDemand")
    public AjaxResult replyEdit(@RequestBody Demand demand){
        return toAjax(demandService.updateAllByDemandId(demand));
    }

    /**
     * 审核
     * @param demand
     * @return
     */
    @PreAuthorize("@ss.hasPermi('demand:needs:audit')")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody Demand demand){
        if(demand.getFlay() == true){
            if(!demand.getAuditStatus().equals("2")){
                if(!demand.getReplyStatus().equals("0")){
                    demand.setAuditStatus("2");
                }else{
                    return warn("该需求未回复！");
                }

            }else{
                return warn("该需求已审核！");
            }
        }else{
            if(!demand.getReplyStatus().equals("0")){
                demand.setAuditStatus("1");
            }else{
                return warn("该需求未回复！");
            }

        }
        return toAjax(demandService.updateAll(demand));
    }

    @PreAuthorize("@ss.hasPermi('demand:needs:finish')")
    @PutMapping("/finish")
    public AjaxResult finish(@RequestBody Demand demand){
        if(demand.getFlay() == true){
            if(demand.getAuditStatus().equals("2")){
                demand.setDemandStatus("2");
            }else{
                if(demand.getAuditStatus().equals("1")){
                    return warn("该需求被管理员拒绝！");
                }else{
                    return warn("该需求未审核！");
                }
            }
        }else{
            if(!demand.getAuditStatus().equals("0")){
                demand.setDemandStatus("1");
            }else{
                return warn("该需求未审核！");
            }

        }
        return toAjax(demandService.updateAll(demand));
    }



}
